<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>重置密码</span>
    </div>
    <div>
      <!-- 内容区放表单 -->
      <el-form label-width="80px" ref="form" :model="pass" :rules="rules">
        <!-- 表单中的每一行，叫做 el-form-item -->
        <el-form-item label="原密码" prop="old_pwd">
          <el-input v-model="pass.old_pwd" show-password></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="new_pwd">
          <el-input v-model="pass.new_pwd" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="re_pwd">
          <el-input v-model="pass.re_pwd" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="updatePwd">确认修改</el-button>
          <!-- <el-button @click="resetFields">重置</el-button> -->
          <el-button @click="$refs.form.resetFields()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-card>
</template>

<script>
import { updatePwdAPI } from '@/api/user'
export default {
  data() {
    // 自定义的规则
    const newPwd = (rule, value, callback) => {
      // value 表示新密码输入框的内容  也就是新密码
      if (value === this.pass.old_pwd) {
        // console.log(1111)
        callback(new Error('新密码和旧密码相同'))
      } else {
        callback()
      }
    }
    const rePwd = (rule, value, callback) => {
      // value 表示确认密码输入框的内容  也就是确认密码
      if (value === this.pass.new_pwd) {
        callback()
      } else {
        callback(new Error('两次输入不一致'))
      }
    }
    return {
      // 数据项
      pass: {
        old_pwd: '',
        new_pwd: '',
        re_pwd: ''
      },
      // 验证规则
      rules: {
        old_pwd: [
          { required: true, message: '请输入旧密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码长度为6 ~ 15位', trigger: 'blur' }
        ],
        new_pwd: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码长度为6 ~ 15位', trigger: 'blur' },
          { validator: newPwd, trigger: 'blur' }
        ],
        re_pwd: [
          { required: true, message: '请再次确认新密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码长度为6 ~ 15位', trigger: 'blur' },
          { validator: rePwd, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    updatePwd() {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        // 验证通过, 提交数据
        const { data: res } = await updatePwdAPI(this.pass)
        // console.log(res)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // ----------- 业务逻辑一: 修改成功后, 重置表单---------
        // this.$refs.form.resetFields()

        // ---------- 业务逻辑二: 重置了密码, 退出, 重新登录---------------
        // 重置vuex中的 toekn, user, 然后跳转到登录页(和退出一样)
        this.$store.commit('user/updateToken', '')
        this.$store.commit('user/updateUser', '')
        this.$router.push('/login')
      })
    }
    // resetFields() {
    //   this.$refs.form.resetFields()
    // }
  }
}
</script>

<style lang="less" scoped>
.el-form {
  width: 500px;
}
</style>
